/*
 * Copyright (C) 2020 1km Holding Limited
 */

#ifndef CAN_BUS_H_
#define CAN_BUS_H_

#include <stdint.h>
#include "aos/kernel.h"
#include "aos/hal/can.h"
#include "aos/list.h"

#define CAN_SEND_BLOCK_MS 1000
#define CAN_BUS_TASK_DEPTH 2048

#ifdef __cplusplus
extern "C" {
#endif

typedef struct{
	int8_t id;//can端口
	
	int32_t (*canbus_send)(const can_frame_t *frame);//can发送函数
	void    *send_mutex;//发送互斥信号量

	can_frame_t *can_rx_pbuf;//can数据接收缓存
	aos_queue_t can_rx_queue;//can数据接收队列

	uint32_t dev_num;//注册的can设备号

	slist_t  bus_l;//can总线链表
	void    *bus_mutex;//can总线互斥信号量
	
} canbus_t;

typedef struct {
	slist_t     next;//指向下一个can设备
	void       *sem_t;//can设备信号量
	uint32_t    rx_id;//can设备的接收can id
	uint8_t    *rx_buf;//can设备接收的数据
    uint8_t     rx_len;//接收长度
	canbus_t   *bus;//can设备的总线信息
} canbus_dev_t;

int32_t canbus_init(void);

extern int canbus_dev_register(canbus_dev_t *dev);
extern int canbus_dev_unregister(canbus_dev_t *dev);
#ifdef __cplusplus
}
#endif

#endif /* CAN_BUS_H_ */

